﻿$("#ProjectName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Project/Search",
            type: "POST",
            dataType: "json",
            data: {
                Keyword: request.term,
                ReturnType: "json"
            },
            success: function (projects) {
                response($.map(projects, function (project) {
                    return {
                        label: project.Name,
                        value: project.Name,
                        projectId: project.Id
                    }
                }));
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {
        $("#ProjectId").val(ui.item.projectId);
        LoadAllDefect(ui.item.projectId);
    },
    open: function () {
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});

function LoadAllDefect(projectId) {
    var type = $("#TypeView").val();
    $.ajax({
        url: "/Defect/GetDefectInProject",
        type: "POST",
        dataType: "json",
        data: {
            ProjectId: projectId,
            Type: type
        },
        success: function (data, status, xht) {
            OnSuccess(data);
        }
    });
}

function OnSuccess(jsonObj) {
    $("tr[id]").remove();
    if (jsonObj.Defects.length > 0) {
        for (i = 0; i < jsonObj.Defects.length; i++) {
            var row = "<tr id='" + jsonObj.Defects[i].DefectId + "'>";
            row += "<td><input type='checkbox' name='DefectId' value='" + jsonObj.Defects[i].DefectId + "'/></td>";
            row += "<td>" + jsonObj.Defects[i].DefectNumber + "</td>";
            row += "<td>" + jsonObj.Defects[i].DefectTitle + "</td>";
            row += "<td>" + jsonObj.Defects[i].Type + "</td>";
            row += "<td>" + jsonObj.Defects[i].ProjectName + "</td>";
            row += "<td>" + jsonObj.Defects[i].Priority + "</td>";
            row += "<td>" + jsonObj.Defects[i].Status + "</td>";
            row += "<td>" + jsonObj.Defects[i].CreatedDate + "</td>";
            row += "<td>" + jsonObj.Defects[i].Creator + "</td>";
            if (jsonObj.Defects[i].AssignedMember != null) {
                row += "<td>" + jsonObj.Defects[i].AssignedMember + "</td>";
            } else {
                row += "<td></td>";
            }
            row += "<td class='options-width'>";
            if (jsonObj.UserName == jsonObj.Defects[i].AssignedMember || jsonObj.UserRole == "Group Admin" || jsonObj.UserRole == "Project Admin")
            {
                row += "<a href='/Defect/Resolution/" + jsonObj.Defects[i].DefectId + "?ReturnUrl=" + $("#RequestUrl").val() + "' title='Edit Solution' class='icon-Solution info-tooltip'></a>";
            }
            if (jsonObj.UserRole != "User" || (jsonObj.UserRole == "User" && (json.UserName == jsonObj.Defects[i].Creator || json.UserName == jsonObj.Defects[i].AssignedMember))){
                if (!jsonObj.Defects[i].IsClose){
                    row += "<a href='/Defect/CloseReopenDefect?id=" + jsonObj.Defects[i].DefectId + "&act=Close&requesturl=" + $("#RequestUrl").val() + "' class='icon-close info-tooltip' title='Close this defect'></a>";
                }
                else{
                    row += "<a href='/Defect/CloseReopenDefect?id=" + jsonObj.Defects[i].DefectId + "&act=ReOpen&requesturl=" + $("#RequestUrl").val() + "' class='icon-open info-tooltip' title='Re-Open this defect'></a>";
                }
            }
            row += "<a href='/Defect/Details/" + jsonObj.Defects[i].DefectId + "' title='View' class='icon-6 info-tooltip'></a>";
            if (jsonObj.UserRole != "User" || json.UserName == defect.Creator || jsonObj.Defects[i].AssignedMember ==json.UserName){
                row += "<a href='/Defect/Edit/" + jsonObj.Defects[i].DefectId + "?ReturnUrl=" + $("#RequestUrl").val() + "' title='Edit' class='icon-1 info-tooltip'></a>";
            }
            if (jsonObj.UserRole != "User" || json.UserName == jsonObj.Defects[i].Creator) {
                row += "<a href='/Defect/DeleteDefect/" + jsonObj.Defects[i].DefectId + "/" + jsonObj.PageIndex + "/" + jsonObj.Type + "' onclick='return confirm(\"Are you sure to delete this defect?\")' title='Delete' class='icon-2 info-tooltip'></a>";
            }
            row += "</td>";
            row += "</tr>";
            $(".product-table").append(row);
        }
        var PageNavText = "";
        if (jsonObj.hasPrevious) {
            if (jsonObj.ProjectId != null) {
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/1/" + jsonObj.ProjectId + "' class='page-far-left'></a>";
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/" + (jsonObj.PageIndex - 1) + "/" + jsonObj.ProjectId + "' class='page-left'></a>";
            } else {
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/1' class='page-far-left'></a>";
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/" + (jsonObj.PageIndex - 1) + "' class='page-left'></a>";
            }
        }
        PageNavText += "<div class='page-info'>Page <strong>" + jsonObj.PageIndex + "</strong> / " + jsonObj.TotalPage + "</div>";
        if (jsonObj.hasNext) {
            if (jsonObj.ProjectId != null) {
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/" + (jsonObj.PageIndex + 1) + "/" + jsonObj.ProjectId + "' class='page-right'></a>";
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/" + jsonObj.TotalPage + "/" + jsonObj.ProjectId + "' class='page-far-right'></a>";
            } else {
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/" + (jsonObj.PageIndex + 1) + "' class='page-right'></a>";
                PageNavText += "<a href='/Defect/Index/" + jsonObj.Type + "/" + jsonObj.TotalPage + "/' class='page-far-right'></a>";
            }
        }
        $("#PageNavigator").html(PageNavText);
    }
}

$("#loading").ajaxStart(function () {
    $(this).center();
    $(this).show();
});

$("#loading").ajaxStop(function () {
    $(this).hide();
});

$("#ProjectName").keyup(function (event) {
    $("#ProjectId").val(null);
    event.preventDefault();
});

$("#ProjectName").focusout(function () {
    if ($("#ProjectId").val() == null || $("#ProjectId").val() == "") {
        LoadAllDefect("");
        $(this).val("");
    }
});